Properties
Property | Class | Structure | Enum | Global | Local | Observer |
---|---|---|---|---|---|---|
Stored | var orlet |
var orlet |
- | yes | ||
Computed | var |
var |
var |
var |
var |
|
Lazy Stored | var |
var |
- | without lazy |
- | no |
inherited | yes | |||||
nonoerridden | no | |||||
Stored type | static |
static |
static |
|||
Computed type | class |
static |
static |
|||
change property | - | mutating |
mutating |
- if instance is
let
,property would not changed
- Observer can be used for global or local variable
Stored Properties
Stored properties can be either variable stored properties or constant stored properties
1 | struct FixedLengthRange { |
Stored Properties of Constant Structure Instances
When an instance of a value type is marked as a constant, so are all of its properties.
If you create an instance of a structure and assign that instance to a constant, you cannot modify the instance’s properties, even if they were declared as variable properties:
1 | let rangeOfFourItems = FixedLengthRange(firstValue: 0, length: 4) |
Lazy Stored Properties
What is Lazy
- A lazy stored property is a property whose initial value is not calculated until the first time it is used. You indicate a lazy stored property by writing the lazy modifier before its declaration.
- You must always declare a lazy property as a variable (with the var keyword), because its initial value might not be retrieved until after instance initialization completes. Constant properties must always have a value before initialization completes, and therefore cannot be declared as lazy.
- If a property marked with the lazy modifier is accessed by multiple threads simultaneously and the property has not yet been initialized, there is no guarantee that the property will be initialized only once.
1 | class DataImporter { |
When to use Lazy
- when the initial value for a property is dependent on outside factors whose values are not known until after an instance’s initialization is complete.
- when the initial value for a property requires complex or computationally expensive setup that should not be performed unless or until it is needed.
- As above , It is possible for a DataManager instance to manage its data without ever importing data from a file
Stored Properties and Instance Variables
- In Objective - C, it has properties and instance variables
- In Swift, it unifies these concepts into a single property declaration
Computed Properties
- do not actually store a value. Instead, they provide a getter and an optional setter to retrieve and set other properties and values indirectly.
- Declare computed properties as
var
1 | struct Point { |
Shorthand Setter Declaration
If a computed property’s setter does not define a name for the new value to be set, a default name of newValue is used.
1 | struct AlternativeRect { |
Read-Only Computed Properties
A computed property with a getter but no setter is known as a read-only computed property.
You can simplify the declaration of a read-only computed property by removing the get keyword and its braces:
1 | struct Cuboid { |
Property Observers
Property observers observe and respond to changes in a property’s value.
What is it for
- stored properties
- global or local variable
- inherited property (whether stored or computed) by overriding the property within a subclass.
What is not for
- not for lazy stored properties.
- not for nonoverridden computed properties, as it can be done by computed property’s setter.
willSet
willSet
is called just before the value is stored.- passed the new property value as a constant parameter.
- specify a name for this parameter or default is
newValue
didSet
didSet
is called immediately after the new value is stored.- passed a constant parameter containing the old property value.
- name the parameter or use the default parameter name of
oldValue
- can assign a value to a property
1 | class StepCounter { |
Global and Local Variables
- computing and observing properties are also available to global variables and local variables.
- Global constants and variables are always computed lazily without
lazy
mark - Local constants and variables are never computed lazily.
Type Properties
Property | var | let | default value |
---|---|---|---|
Stored type | yes | yes | must |
Computed type | yes | - |
- Type properties are useful for defining values that are universal to all instances of a particular type,
- Stored type properties can be variables or constants.
- Computed type properties are always declared as variable properties
- Unlike stored instance properties, you must always give stored type properties a default value. This is because the type itself does not have an initializer that can assign a value to a stored type property at initialization time.
- Stored type properties are lazily initialized on their first access. They are guaranteed to be initialized only once, even when accessed by multiple threads simultaneously, and they do not need to be marked with the lazy modifier.
Type Property Syntax
- In C and Objective-C, you define static constants and variables associated with a type as global static variables
- In Swift, however, type properties are written as part of the type’s definition,
- For computed type properties for class types, you can use the
class
keyword instead to allow subclasses to override the superclass’s implementation
1 | struct SomeStructure { |
Querying and Setting Type Properties
Querying syntax
1 | print(SomeStructure.storedTypeProperty) |
Example
1 | struct AudioChannel { |